﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Milkify.Interfaces;
using Milkify.Data;
using IronCow;
using System.Windows.Input;
using Milkify.Input;

namespace Milkify.Controllers
{
    class WeekController : Controller, IController
    {
        private IRefreshableView mView;

        public WeekController(ControllerContext context, Rtm rtm, IViewContainer viewContainer, IViewFactory viewFactory)
            : base(context, rtm, viewContainer)
        {
            mView = (IRefreshableView)viewFactory.Create("Week");
            mView.Loaded += new System.Windows.RoutedEventHandler(View_Loaded);
            mView.NeedsRefresh = true;
        }

        public override void Execute()
        {
            // Create commands.
            ICommand navigateToTaskListCommand = new DecoupledCommand<object>(NavigateToTaskList);

            // Create view data.
            DateTime today = DateTime.Today;
            var viewData = new WeekViewData();
            viewData.Today = new TaskListViewData("Today", "status:incomplete AND dueBefore:tomorrow");
            viewData.Tomorrow = new TaskListViewData("Tomorrow", "status:incomplete AND due:tomorrow");
            viewData.TwoDays = new TaskListViewData(today.AddDays(2).DayOfWeek.ToString(), "status:incomplete AND due:\"2 days\"");
            viewData.ThreeDays = new TaskListViewData(today.AddDays(3).DayOfWeek.ToString(), "status:incomplete AND due:\"3 days\"");
            viewData.FourDays = new TaskListViewData(today.AddDays(4).DayOfWeek.ToString(), "status:incomplete AND due:\"4 days\"");
            viewData.FiveDays = new TaskListViewData(today.AddDays(5).DayOfWeek.ToString(), "status:incomplete AND due:\"5 days\"");
            viewData.SixDays = new TaskListViewData(today.AddDays(6).DayOfWeek.ToString(), "status:incomplete AND due:\"6 days\"");
            viewData.NavigateToTaskListCommand = navigateToTaskListCommand;
            mView.ViewData = viewData;

            ViewContainer.NavigateToView(mView);
        }

        private void View_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (mView.NeedsRefresh)
            {
                RefreshLists();
                mView.NeedsRefresh = false;
            }
        }

        private void RefreshLists()
        {
            WeekViewData viewData = mView.ViewData as WeekViewData;
            if (viewData == null)
                throw new InvalidOperationException("The view data for the week view hasn't been set yet.");

            viewData.Today.RefreshAsync(Rtm);
            viewData.Tomorrow.RefreshAsync(Rtm);
            viewData.TwoDays.RefreshAsync(Rtm);
            viewData.ThreeDays.RefreshAsync(Rtm);
            viewData.FourDays.RefreshAsync(Rtm);
            viewData.FiveDays.RefreshAsync(Rtm);
            viewData.SixDays.RefreshAsync(Rtm);
        }

        private void NavigateToTaskList(object parameter)
        {
            Redirect("TaskList", "NavigateToTaskList", parameter);
        }

        private void Refresh(object sender, ExecutedRoutedEventArgs e)
        {
            RefreshLists();
            e.Handled = true;
        }
    }
}
